% This file is derived from http://www.cs.toronto.edu/cogrobo/kia/simpleElevator
%

                   %  THE ELEVATOR CONTROLLER
                   %       ..in YAGI
				   
				   
%% Primitive control actions

primitive_action(turnoff(_N)).  % Turn off call button N.
primitive_action(open).        % Open elevator door.
primitive_action(close).       % Close elevator door.
primitive_action(up(_N)).       % Move elevator up to floor N.
primitive_action(down(_N)).     % Move elevator down to floor N.


%% Signals

signal(turnoff(N),S):-
	text_term(NS,N), text_concat('Turn-off button at floor ', NS, S).
signal(up(N),S):-
	text_term(NS,N), text_concat('Move up to floor ', NS, S).
signal(down(N),S):-
	text_term(NS,N), text_concat('Move down to floor ', NS, S).
signal(open,'Open door').
signal(close, 'Close door').

%% Complex control actions

proc(goto(N), ?(fcurrFloor(N)) # up(N) # down(N)).
proc(serve(N), goto(N) : turnoff(N) : open : close).
proc(serveafloor, pi(n, ?(fon(n)) : serve(n))).
proc(park, if(fcurrFloor(0), open, down(0) : open)).


%% Main controller
%% As long as there is an active call button, 
%% it serves one floor. When all buttons are off, it
%% parks the elevator.

proc(control, while(some(n, fon(n)), serveAfloor) : park).


%% Preconditions for Primitive Actions.

poss(up(N),S) :- fcurrFloor(M,S), M < N.
poss(down(N),S) :- fcurrFloor(M,S), M > N.
poss(open,_S).
poss(close,_S).
poss(turnoff(N),S) :- fon(N,S).


%% Successor State Axioms for Fluents.

fcurrFloor(M,do(A,S)) :- A = up(M) ; A = down(M) ;
              \+ A = up(N), \+ A = down(N), fcurrFloor(M,S).

fon(M,do(A,S)) :- fon(M,S), \+ A = turnoff(M).


%% Initial Situation. Call buttons: 3 and 5. The elevator is at floor 4.

fon(3,s0).   
fon(5,s0).   
fcurrFloor(4,s0).

ffloors(1,s0).
ffloors(2,s0).
ffloors(3,s0).
ffloors(4,s0).
ffloors(5,s0).
ffloors(6,s0).

%% Restore suppressed situation arguments.

restoreSitArg(fon(N),S,fon(N,S)).
restoreSitArg(fcurrFloor(M),S,fcurrFloor(M,S)).


